package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.queryObject.DestinationQueryObject;
import cn.wolfcode.wolf2w.queryObject.RegionQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Autowired
    private IDestinationService destinationService;

    /**
     * 添加关联目的地的下拉框
     * @return
     */
    @RequestMapping("/listDests")
    @ResponseBody
    public Object listDests(){
        return destinationService.list();
    }

    /**
     * 分页映射方法
     * @param model
     * @param qo    分页条件
     * @return
     */
    @RequestMapping("/list")
    public Object list(Model model, @ModelAttribute("qo")DestinationQueryObject qo){
        // Page : spring-data 提供用于 mongodb 分页的类
        List<Destination> toasts = destinationService.getToasts(qo.getParentId());
        Page<Destination> page = destinationService.query(qo);
        model.addAttribute("page",page);
        model.addAttribute("toasts",toasts);
        return "destination/list";
    }

    /**
     * 编辑目的地简介
     * @return
     */
    @RequestMapping("/updateInfo")
    @ResponseBody
    public Object updateInfo(String id,String info){

        destinationService.updateInfo(id,info);

        return JsonResult.success();
    }

}
